﻿using HDS.Data;
using HDS.IService.Master;
using HDS.Model;
using HDS.Model.Entity.Master;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HDS.Service.Master
{
    public class GeoService : IGeoService
    {
        private MasterDbContext db = new MasterDbContext();

        public async Task<DataPagingModel<City>> GetCityList(int pageIndex, int pageSize, Expression<Func<City, bool>> whereExpression, string whereSql)
        {
            RefAsync<int> total = 0;
            var rows = await db.Db.Queryable<City>()
                .WhereIF(whereExpression != null, whereExpression)
                .WhereIF(!string.IsNullOrEmpty(whereSql), whereSql)
                .OrderBy(e => e.CityNameEn, OrderByType.Asc)
                .ToPageListAsync(pageIndex, pageSize, total);

            return new DataPagingModel<City>
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                PageTotal = (total.Value + pageSize - 1) / pageSize,
                Total = total.Value,
                Rows = rows
            };
        }

        public async Task<List<City>> GetCityList(Expression<Func<City, bool>> whereExpression, string whereSql)
        {
            RefAsync<int> total = 0;
            var rows = await db.Db.Queryable<City>()
                .WhereIF(whereExpression != null, whereExpression)
                .WhereIF(!string.IsNullOrEmpty(whereSql), whereSql)
                .OrderBy(e => e.CityNameEn, OrderByType.Asc)
                .ToListAsync();

            return rows;
        }

        public async Task<List<Country>> GetCountryList(Expression<Func<Country, bool>> whereExpression)
        {
            return await db.Db.Queryable<Country>().WhereIF(whereExpression != null, whereExpression).OrderBy(e => e.CountryNameEn, OrderByType.Asc).ToListAsync();
        }
    }
}
